package com.hitqz.robot.biz.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.cache.Cache;
import com.google.common.collect.Maps;
import com.hitqz.robot.api.common.dto.mapArea.CalculateAreaMapDto;
import com.hitqz.robot.api.common.dto.robot.RobotPosInfoDto;
import com.hitqz.robot.api.common.entity.MapArea;
import com.hitqz.robot.api.common.entity.MapAreaPos;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.util.CacheNameUtil;
import com.hitqz.robot.biz.business.model.Line;
import com.hitqz.robot.biz.service.RobotService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.algorithm.distance.DistanceToPoint;
import org.locationtech.jts.algorithm.distance.PointPairDistance;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.WKTReader;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author xupkun
 * @date 2024/6/11
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PosCalculateSchedule {


    private final RobotService robotService;
    /**
     * 预测靠近的区域
     */
    private final MapArea predictionArea = null;

    private final Map<Integer, List<Line>> areaLines = Maps.newHashMap();

    private final GeometryFactory geometryFactory = new GeometryFactory();

    //@Scheduled(cron = "0/1 * * * * ?")
    public void schedule() {
        Cache<String, RobotPosInfoDto> posInfoDtoCache = SpringUtil.getBean(CacheNameUtil.CACHE_POS_INFO);
        Robot robot = robotService.getDefaultRobot();
        for (String pos : posInfoDtoCache.asMap().keySet()) {
            RobotPosInfoDto dto = posInfoDtoCache.getIfPresent(pos);
            Cache<Long, CalculateAreaMapDto> areaMapDtoCache = SpringUtil.getBean(CacheNameUtil.CACHE_MAP_AREA);
            for (Long key : areaMapDtoCache.asMap().keySet()){
                CalculateAreaMapDto mapDto = areaMapDtoCache.getIfPresent(key);
                List<MapAreaPos> mapAreaPos = mapDto.getMapAreaPosList();
                if (CollectionUtil.isNotEmpty(mapAreaPos)) {

                    // 创建两个多边形对象
                    // 创建两个多边形对象
                    Polygon polygon1 = geometryFactory.createPolygon(new Coordinate[] {
                            new Coordinate(10, 20),
                            new Coordinate(10, 40),
                            new Coordinate(30, 40),
                            new Coordinate(30, 20),
                            new Coordinate(10, 20)
                    });

                    Polygon polygon2 = geometryFactory.createPolygon(new Coordinate[] {
                            new Coordinate(20, 30),
                            new Coordinate(20, 50),
                            new Coordinate(40, 50),
                            new Coordinate(40, 30),
                            new Coordinate(20, 30)
                    });

                    // 判断是否相交
                    boolean intersects = polygon1.intersects(polygon2);
                    System.out.println("Intersects: " + intersects);

                    // 判断是否包含
                    boolean contains = polygon1.contains(polygon2);
                    System.out.println("Contains: " + contains);

                    // 判断是否重叠
                    boolean overlaps = polygon1.overlaps(polygon2);
                    System.out.println("Overlaps: " + overlaps);

//------------------------------------------------------------------------------------------------
                    Point point = geometryFactory.createPoint(new Coordinate(10, 20));

                    // 创建线串对象
                    LineString lineString = geometryFactory.createLineString(new Coordinate[] {
                            new Coordinate(10, 20),
                            new Coordinate(30, 40)
                    });
                    Polygon polygon = geometryFactory.createPolygon(new Coordinate[] {
                            new Coordinate(10, 20),
                            new Coordinate(10, 40),
                            new Coordinate(30, 40),
                            new Coordinate(30, 20),
                            new Coordinate(10, 20)
                    });

                    PointPairDistance pointPairDistance = new PointPairDistance();
                    WKTReader wktReader = new WKTReader();
                    //线L1
                    //LineString lineString =  (LineString) wktReader.read("LINESTRING (103.823703 36.058191,103.823676 36.054882,103.829764 36.054903)");
                    //点p1
                    //Point point = (Point) wktReader.read("POINT (103.827083 36.057880)");
                    DistanceToPoint.computeDistance(lineString, point.getCoordinate(),pointPairDistance);
                    System.out.println("距离："+pointPairDistance.getDistance()*100000+"米，垂足点："+pointPairDistance.getCoordinate(0));



                    // 缓冲区操作
                    Geometry bufferedPolygon = polygon.buffer(5);
                    System.out.println("Buffered Polygon: " + bufferedPolygon);

                    // 相交操作
                    Geometry intersection = point.intersection(lineString);
                    System.out.println("Intersection: " + intersection);

                    // 并集操作
                    Geometry union = point.union(lineString);
                    System.out.println("Union: " + union);

                    // 差集操作
                    Geometry difference = polygon.difference(lineString);
                    System.out.println("Difference: " + difference);


                    //List<Point> pts = mapAreaPos.stream().filter(t-> t.getPosx() != null && t.getPosy() != null).
                    //        map(t-> new Point(t.getPosx(), t.getPosy())).collect(Collectors.toList());
                    //Line line = CalculateUtil.calPtsDistance(new Point(dto.getX(), dto.getY()), pts);
                    //if (line.getStartPos() != null && line.getEndPos() != null) {
                    //    Point point = CalculateUtil.getFoot(line, dto.getX(), dto.getY());
                    //    log.info("机器人距离{} --- {}--- {} ", key, CalculateUtil.isOnLine(line, point.getX(), point.getY()),
                    //            CalculateUtil.isPtInPoly(new Point(dto.getX(), dto.getY()), pts));
                    //}
                }
            }
        }
    }


}
